//package com.sf.fy.uav.applet.netty;
//
//import io.netty.bootstrap.ServerBootstrap;
//import io.netty.channel.*;
//import io.netty.channel.nio.NioEventLoopGroup;
//import io.netty.channel.socket.SocketChannel;
//import io.netty.channel.socket.nio.NioServerSocketChannel;
//import io.netty.handler.codec.string.StringDecoder;
//import io.netty.handler.codec.string.StringEncoder;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.context.ApplicationListener;
//import org.springframework.context.event.ContextRefreshedEvent;
//import org.springframework.stereotype.Component;
//
///**
// * @author Siybom
// */
//@Component
//public class NettyTcpServer implements ApplicationListener<ContextRefreshedEvent> {
//
//    @Value("${netty.tcp.port}")
//    private int port;
//
//
//    @Override
//    public void onApplicationEvent(ContextRefreshedEvent event) {
//        start();
//    }
//
//    private void start() {
//        EventLoopGroup bossGroup = new NioEventLoopGroup();
//        EventLoopGroup workerGroup = new NioEventLoopGroup();
//        try {
//            ServerBootstrap b = new ServerBootstrap();
//            b.group(bossGroup, workerGroup)
//                    .channel(NioServerSocketChannel.class)
//                    .childHandler(new ChannelInitializer<SocketChannel>() {
//                        @Override
//                        public void initChannel(SocketChannel ch) throws Exception {
//                            ChannelPipeline p = ch.pipeline();
//                            p.addLast(new StringDecoder());
//                            p.addLast(new StringEncoder());
//                            p.addLast(new SimpleChannelInboundHandler<String>() {
//                                @Override
//                                protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
//                                    System.out.println("Server received: " + msg);
//                                    ctx.writeAndFlush("Server response: " + msg);
//                                }
//
//                                @Override
//                                public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
//                                    cause.printStackTrace();
//                                    ctx.close();
//                                }
//                            });
//                        }
//                    });
//
//            b.bind(port).sync().channel().closeFuture().sync();
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            bossGroup.shutdownGracefully();
//            workerGroup.shutdownGracefully();
//        }
//    }
//}
